package org.example.crop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.crop.domain.MarketCrop;
import org.example.crop.domain.StashCrop;
import org.example.crop.service.MarketCropService;
import org.example.crop.service.StashCropService;
import org.example.crop.mapper.StashCropMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author scf
* @description 针对表【t_stash_crop】的数据库操作Service实现
* @createDate 2024-05-24 19:46:30
*/
@Service
public class StashCropServiceImpl extends ServiceImpl<StashCropMapper, StashCrop>
    implements StashCropService{
    @Autowired
    private MarketCropService marketCropService;
    @Override
    public List<StashCrop> getList() {
        return baseMapper.getList();
    }

    @Override
    @Transactional
    public boolean outStash(StashCrop stashCrop) {
        System.out.println(stashCrop);
        boolean save =false;
        StashCrop stashCrop1 = baseMapper.getListByCropId(stashCrop);
        if(stashCrop1.getNum()==stashCrop.getNum()){
            int num = stashCrop.getNum() - stashCrop1.getNum();
            save = this.removeById(stashCrop1);
            MarketCrop marketCrop = new MarketCrop();
            marketCrop.setCropId(stashCrop1.getCropId());
            marketCrop.setMarketId(stashCrop.getMarketId());
            marketCrop.setNum(stashCrop.getNum());
            save = marketCropService.save(marketCrop);
            return save;
        }
        StashCrop stashCrop2 =null;
        if(stashCrop1.getNum()<stashCrop.getNum()){
            int num = stashCrop.getNum() - stashCrop1.getNum();
            save = this.removeById(stashCrop1);
            stashCrop2 = baseMapper.getListByCropId(stashCrop);
            stashCrop2.setNum(stashCrop2.getNum()-(num));
            save= this.updateById(stashCrop2);
            MarketCrop marketCrop = new MarketCrop();
            marketCrop.setCropId(stashCrop1.getCropId());
            marketCrop.setMarketId(stashCrop.getMarketId());
            marketCrop.setNum(stashCrop.getNum());
            save = marketCropService.save(marketCrop);
            return save;
        }
        stashCrop2 = baseMapper.getListByCropId(stashCrop);
        stashCrop2.setNum(stashCrop2.getNum()-(stashCrop.getNum()));
        save= this.updateById(stashCrop2);
        MarketCrop marketCrop = new MarketCrop();
        marketCrop.setCropId(stashCrop1.getCropId());
        marketCrop.setMarketId(stashCrop.getMarketId());
        marketCrop.setNum(stashCrop.getNum());
        save = marketCropService.save(marketCrop);
        return save;
    }

    @Override
    public StashCrop getListByCropId(StashCrop stashCrop) {
        return baseMapper.getListByCropId(stashCrop);
    }
}




